package com.huatao.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.huatao.auth.application.convert.AuthPrimissionDTOToConverter;
import com.huatao.auth.application.dto.AuthPermissionDTO;
import com.huatao.auth.domain.bo.AuthPermissionBO;
import com.huatao.auth.domain.service.AuthPermissionDomainService;
import com.huatao.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/primission")
@Slf4j
public class PrmissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    @PostMapping("addPrimission")
    public Result<Boolean> addPrimission(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PrmissionController addPrimission: {}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()), "name is blank");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "权限父id不能为空");
            AuthPermissionBO authPermissionBO = AuthPrimissionDTOToConverter.INSTANCE.convertDtoToBo(authPermissionDTO);
            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        } catch (Exception e) {
            log.error("addPrimission error", e);
            return Result.fail("addPrimission error");
        }
    }

    @PostMapping("updatePrimission")
    public Result<Boolean> updatePrimission(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PrmissionController updatePrimission: {}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPrimissionDTOToConverter.INSTANCE.convertDtoToBo(authPermissionDTO);
            return Result.ok(authPermissionDomainService.update(authPermissionBO));
        } catch (Exception e) {
            log.error("updatePrimission error", e);
            return Result.fail("updatePrimission error");
        }
    }

    @PostMapping("deletePrimission")
    public Result<Boolean> deletePrimission(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PrmissionController deletePrimission: {}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPrimissionDTOToConverter.INSTANCE.convertDtoToBo(authPermissionDTO);
            return Result.ok(authPermissionDomainService.delete(authPermissionBO));
        } catch (Exception e) {
            log.error("deletePrimission error", e);
            return Result.fail("deletePrimission error");
        }
    }

    @PostMapping("getPermission")
    public Result<Boolean> getPermission(String userName) {
        try {
            log.info("PrmissionController getPermission: {}", userName);
            Preconditions.checkArgument(!StringUtils.isBlank(userName), "用户名不能为空");
            return Result.ok(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("getPermission error", e);
            return Result.fail("获取用户权限失败");
        }
    }
}
